import {Component, OnInit, TemplateRef, ViewChild} from '@angular/core';
import {ColumnType, SPage, TablePageComponent} from "@shared/components/table-page/table-page.component";
import {_HttpClient} from "@delon/theme";
import {NzMessageService, NzModalService} from "ng-zorro-antd";
import {ApiService} from "@core/services/api";
import {AddComponent} from "../../per-group/per-list/add/add.component";

@Component({
  selector: 'app-dept-list',
  templateUrl: './dept-list.component.html',
  styles: []
})
export class DeptListComponent implements OnInit {
  @ViewChild('st', {static: false})st: TablePageComponent;
  loading = false;
  // 已选数据
  selectedData: Array<any>;
  addDataList = {
    bmbh:'',
    bmmc:[]
  };
  config: SPage = {
    columns: [
      {
        index: 'id',
        title: '多选',
        type: ColumnType.checkbox,
      },
      {
        index: 'deptName',
        title: '名称',
        filter: {
          placeholder: '名称',
          type: 'string'
        }
      },
      {
        index: 'deptFullName',
        title: '全称',
        filter: {
          placeholder: '全称',
          type: 'string'
        }
      },

      {
        index: 'status',
        title: '状态',
        type: ColumnType.tag,
        tag: {
          false: {text:'停用', color: 'red'},
          true: {text:'启用', color: 'green'},
        },
        filter: {
          placeholder: '',
          type: 'select',
          items: [
            {label: '启用', value: true},
            {label: '停用', value: false},
          ]
        }
      },
      {
        index: 'createTime',
        title: '创建时间',
        filter: {
          placeholder: 'YYYY-MM-dd 00:00',
          type: 'string'
        }
      },
      {
        index: 'remark',
        title: '备注',
      },
    ],
    editBtn: [
      {title: '编辑'}
    ],
    reqUrl: this.api.dept.page.url
  }
  // 设置部门树状组件
  nodes = [
    {
      title: '0-0',
      key: '0-0',
      children: [
        {
          title: '0-0-0',
          key: '0-0-0',
          children: [
            { title: '0-0-0-0', key: '0-0-0-0', isLeaf: true },
            { title: '0-0-0-1', key: '0-0-0-1', isLeaf: true },
            { title: '0-0-0-2', key: '0-0-0-2', isLeaf: true }
          ]
        },
        {
          title: '0-0-1',
          key: '0-0-1',
          children: [
            { title: '0-0-1-0', key: '0-0-1-0', isLeaf: true },
            { title: '0-0-1-1', key: '0-0-1-1', isLeaf: true },
            { title: '0-0-1-2', key: '0-0-1-2', isLeaf: true }
          ]
        },
        {
          title: '0-0-2',
          key: '0-0-2',
          isLeaf: true
        }
      ]
    },
    {
      title: '0-1',
      key: '0-1',
      children: [
        { title: '0-1-0-0', key: '0-1-0-0', isLeaf: true },
        { title: '0-1-0-1', key: '0-1-0-1', isLeaf: true },
        { title: '0-1-0-2', key: '0-1-0-2', isLeaf: true }
      ]
    },
    {
      title: '0-2',
      key: '0-2',
      isLeaf: true
    }
  ];

  constructor(private http: _HttpClient, private modal: NzModalService,private message: NzMessageService, private api: ApiService) { }

  ngOnInit() {
  }
  // 编辑功能
  editDic(type: 0 | 1 = 0, _param?: object): void {
    this.modal.create({
      nzTitle: type === 0 ? '新增部门' : '修改部门',
      nzContent: AddComponent,
      nzWidth: 800,
      nzComponentParams: {
        type,
        _param
      },
      nzOnOk: () => {
        this.st.reload();
      },
      nzFooter: null
    })
  }
  operate(e: any): void {

    if (e.title === '编辑') {
      this.editDic(1 , e.data);
    }
  }
  // 多选功能
  _console(e: any): void {
    this.selectedData = e;

  }
  // 新增功能
  addData (templ: TemplateRef<{}>): void {
    this.modal.create({
      nzTitle:'添加部门',
      nzWidth:800,
      nzContent: templ,
      nzOnOk: () => {
        this.loading = true;
        this.message.success('添加成功');
        this.st.reload();
        // this.http.post('', { description: this.addDataList }).subscribe((res) => {
        //   console.log(res);
        // }
        // );
      },
    })
  }
  // 设置部门树组件变更回调函数
  onChange($event: string[]): void {
    this.addDataList.bmmc = $event;
  }
  // 删除功能
  delData(){
    if(!!this.selectedData && this.selectedData.length !== 0){
      this.modal.confirm({
        nzTitle: '您确定要删除这些数据吗',
        nzOnOk: () => {
          const body = [];
          this.selectedData.forEach(item => {
            body.push({id: item.id})
          })
          this.http.put(this.api.permision.delete.url,body).subscribe(res => {
            if (res.status === '200') {
              this.message.success('删除成功');
              this.st.reload();
            }
          })
        }
      })
    }else {
      this.message.warning('请选择数据');
    }
  }

}
